package com.foodost.idiomer;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalDb.DbUpdateListener;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

public class InitDataService extends Service {
    
    private static final String TAG = "InitDataService";
    private IdiomInitTask idiomTask;
    private ExplanInitTask explanTask;
    private boolean isNeed = false;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       
       Log.d(TAG, "read end = " + SystemClock.currentThreadTimeMillis());
       if(isNeed){
           boolean isInitedEplan = false;
           if(intent != null){
               isInitedEplan = intent.getBooleanExtra(Constants.EXPLAN_INITED_FAILED, true);
           }
           if(isInitedEplan){
               idiomTask = new IdiomInitTask();
               idiomTask.execute(InitDataService.this);
           }
           explanTask = new ExplanInitTask();
           explanTask.execute(InitDataService.this);
       }else{
           IdiomModel model = new IdiomModel(this);
           Log.d(TAG, "start time");
           model.copyDatabaseFile(this, true);
           Utils.idiomInitFilish(this);
           Log.d(TAG, "end time");
           Intent in = new Intent();
           in.setClass(this, MainActivity.class);
           in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           this.startActivity(in);
       }
        return super.onStartCommand(intent, flags, startId);
    }

}
